package org.jeecg.modules.tencent_esign.tencent_contract.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.tencent_esign.tencent_contract.entity.CreateFlowDTO;
import org.jeecg.modules.tencent_esign.tencent_contract.entity.TemplateInfoDTO;
import org.jeecg.modules.tencent_esign.tencent_contract.entity.TencentContractInfo;
import org.jeecg.modules.tencent_esign.tencent_contract.service.ITencentContractInfoService;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 电子签合同记录表
 * @Author: jeecg-boot
 * @Date:   2025-07-10
 * @Version: V1.0
 */
@Api(tags="电子签合同记录表")
@RestController
@RequestMapping("/tencent_esign/tencent_contract/tencentContractInfo")
@Slf4j
public class TencentContractInfoController extends JeecgController<TencentContractInfo, ITencentContractInfoService> {
	@Autowired
	private  ITencentContractInfoService tencentContractInfoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param tencentContractInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "电子签合同记录表-分页列表查询")
	@ApiOperation(value="电子签合同记录表-分页列表查询", notes="电子签合同记录表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<TencentContractInfo>> queryPageList(TencentContractInfo tencentContractInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TencentContractInfo> queryWrapper = QueryGenerator.initQueryWrapper(tencentContractInfo, req.getParameterMap());
		Page<TencentContractInfo> page = new Page<TencentContractInfo>(pageNo, pageSize);
		IPage<TencentContractInfo> pageList = tencentContractInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param tencentContractInfo
	 * @return
	 */
	@AutoLog(value = "电子签合同记录表-添加")
	@ApiOperation(value="电子签合同记录表-添加", notes="电子签合同记录表-添加")
	//@RequiresPermissions("tencent_contract:tencent_contract_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TencentContractInfo tencentContractInfo) {
		tencentContractInfoService.save(tencentContractInfo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param tencentContractInfo
	 * @return
	 */
	@AutoLog(value = "电子签合同记录表-编辑")
	@ApiOperation(value="电子签合同记录表-编辑", notes="电子签合同记录表-编辑")
	//@RequiresPermissions("tencent_contract:tencent_contract_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TencentContractInfo tencentContractInfo) {
		tencentContractInfoService.updateById(tencentContractInfo);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "电子签合同记录表-通过id删除")
	@ApiOperation(value="电子签合同记录表-通过id删除", notes="电子签合同记录表-通过id删除")
	//@RequiresPermissions("tencent_contract:tencent_contract_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		tencentContractInfoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "电子签合同记录表-批量删除")
	@ApiOperation(value="电子签合同记录表-批量删除", notes="电子签合同记录表-批量删除")
	//@RequiresPermissions("tencent_contract:tencent_contract_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tencentContractInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "电子签合同记录表-通过id查询")
	@ApiOperation(value="电子签合同记录表-通过id查询", notes="电子签合同记录表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TencentContractInfo> queryById(@RequestParam(name="id",required=true) String id) {
		TencentContractInfo tencentContractInfo = tencentContractInfoService.getById(id);
		if(tencentContractInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tencentContractInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tencentContractInfo
    */
    //@RequiresPermissions("tencent_contract:tencent_contract_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TencentContractInfo tencentContractInfo) {
        return super.exportXls(request, tencentContractInfo, TencentContractInfo.class, "电子签合同记录表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("tencent_contract:tencent_contract_info:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TencentContractInfo.class);
    }

	 /**
	  * 调用腾讯电子签查询模板信息列表接口
	  *
	  * @param request
	  * @param response
	  * @return HttpServletRequest request, HttpServletResponse response
	  */

	 // 企业方信息
	 private static final String OPERATOR_ID = "123456789"; // 操作人ID
	 @ApiOperation(value="腾讯电子签-合同模板查询信息列表接口", notes="腾讯电子签合同模板查询信息列表接口(CreateFlowDTO对象必传属性OperatorId\t操作人id\n" +
			 "必传ProxyOrganizationOpenId\t子客企业创建登录链接时给子企业起的一个代号\n" +
			 "非必传TemplateId\t模板id)")
	 @PostMapping("/getDescribeTemplates")
	 public  Result<List<TemplateInfoDTO>> getDescribeTemplates(CreateFlowDTO createFlowDTO) {
         Result<List<TemplateInfoDTO>> result = tencentContractInfoService.getDescribeTemplates(createFlowDTO);
         return result;
     }

	 /**
	  * 生成子客登录链接接口
	  */
	 @ApiOperation(value="腾讯电子签-生成子客登录链接接口", notes="生成子客登录链接接口(CreateFlowDTO对象必传属性\n" +
			 "OperatorId\t操作人id\n" +
			 "ProxyOrganizationOpenId\t子客企业创建登录链接时给子企业起的一个代号，需要自己定义\n" +
			 "ProxyOrganizationName\t子客企业创建登录链接时自定义的名称（最好与营业执照名称一致）\n" +
			 "OperatorName\t操作人姓名\n" +
			 "OperatorTel\t操作人电话)")
	 @PostMapping("/getCreateConsoleLoginUrl")
	 public  Result<String> getCreateConsoleLoginUrl(@RequestBody CreateFlowDTO createFlowDTO) throws TencentCloudSDKException {
		 Result<String> result = tencentContractInfoService.getCreateConsoleLoginUrl(createFlowDTO);
		 return result;
	 }

	 /**
	  * 调用腾讯电子签创建签署流程CreateFlowsByTemplates接口
	  *
	  * @param createFlowDTO
	  * @return
	  */
	 @ApiOperation(value="腾讯电子签-创建签署流程", notes="腾讯电子签创建签署流程（CreateFlowDTO对象必传属性flowName\t合同名称flowDescription\t合同描述templateId\t合同模板id\t" +
			 "ProxyOrganizationOpenId\t子客企业创建登录链接时给子企业起的一个代号\n" +
			 "ProxyOrganizationName\t子客企业创建登录链接时自定义的名称（最好与营业执照名称一致）\n" +"operatorId操作人id\toperatorName\t操作人姓名operatorTel\t操作人联系方式signers\t签署人对象信息formFields\t合同控件信息\n）")
	 @PostMapping("/createFlowByTemplate")
	 public  Result<String> createFlowByTemplate(CreateFlowDTO createFlowDTO) {
		 try {

			 Result<String> result = tencentContractInfoService.createFlowByTemplate(createFlowDTO);
			 return result;

		 } catch (Exception e) {
			 log.error("腾讯电子签合同创建失败", e);
			 return Result.error("腾讯电子签合同创建失败: " + e.getMessage());
		 }
	 }

	 /**
	  * 给个人/自然人生成签署链接
	  *
	  * @param createFlowDTO
	  * @return
	  */
	 @ApiOperation(value="腾讯电子签-给个人/自然人生成签署链接", notes="给个人/自然人生成签署链接(CreateFlowDTO对象必传属性" +
			 "ProxyOrganizationOpenId\t子客企业创建登录链接时给子企业起的一个代号\n" +
			 "OperatorId\t操作人id，ContractId\t合同id，Signers.Name\t签署人姓名，Signers.Mobile\t签署人联系电话\n)")
	 @PostMapping("/createPersonSignLink")
	 public  Result<String> createPersonSignLink(CreateFlowDTO createFlowDTO) {
		 try {
			 Result<String> result = tencentContractInfoService.createPersonSignLink(createFlowDTO);
			 return result;
		 } catch (Exception e) {
			 log.error("腾讯电子签合同创建失败", e);
			 return Result.error("腾讯电子签合同创建失败: " + e.getMessage());
		 }
	 }
	 /**
	  * 正式的回调url
	  */
	 @ApiOperation(value="正式的回调url", notes="正式的回调url")
	 @PostMapping("/over")
	 @ResponseBody
	 public Result<?> handleCallback(HttpServletRequest request,
									 @RequestBody String body){
		 try {
			// System.out.println("handleCallback-request: " + request);
			// System.out.println("handleCallback-body: " + body);
			 //解密
			 Result<String> result = tencentContractInfoService.handleCallback(request,body);
//			 System.out.println("回调成功!!");
			 return result;
		 } catch (Exception e) {
			 System.out.println("handleCallback-request: " + request);
			 System.out.println("handleCallback-body: " + body);
			 log.error("回调失败", e);
			 return Result.error("腾讯电子签回调失败: " + e.getMessage());
		 }
	 }

}
